var addCategory={
    switchInt:function () {
        //初始化图片展示
        if("undefined" != typeof itemList && "" != itemList && null != itemList){
            $("#case").attr("data-value",itemList.icon);
        }

        $("#case").upload(
            function(_this,data){
                var imgNameStr= data.substring(data.lastIndexOf("/")+1,data.lastIndexOf("."));
                var is = $("#imgDesDiv").children(".panel-body");
                $.each(is,function(i,item){
                    $(item).hide();
                });
                layer.msg("图片链接："+data,{time: 5000},function(){
                });
            }
        )
        $("#categoryName").val("");
        $("#categoryLevel").val("");
        $("#sn").val("");
        sessionStorage.removeItem("orgId");
        $("#cancel").click(function(){
            loadUtil = new Load($);
            loadUtil.load({
                container:'#homeContent',
                url: application.contextPath+"/category/showCategoryPage?menuId="+menuId
            });
            return false;
        })
        if("undefined" != typeof categoryId && "" != categoryId && null != categoryId) {
            $.ajax({
                url: application.contextPath + "/category/queryCategoryById",
                type: 'POST',
                dataType: 'json',
                data: {categoryId: categoryId},
                success: function (res) {
                    if (null != res.data) {
                        var data = res.data;
                        var data2 = res.data;
                        $("#categoryName").val(data.cateName);
                        $("#categoryType").val(data.cateType);
                        $("#categoryLevel").val(data.level);
                        $("#sn").val(data.sn);

                        $("#parentInfo").html("");
                        $.ajax({
                            url:application.contextPath+"/category/queryCategoryAll",
                            type: 'POST',
                            dataType: 'json',
                            data:{"cateType":data.cateType,"categoryId":""},
                            success: function (res) {
                                if(null != res.data){
                                    $("#parentInfo").html("");
                                    $("#parentInfo").append("<option value='0'>根节点</option>");
                                    for (var i=0; i<res.data.rows.length;i++) {
                                        $("#parentInfo").append("<option value='"+res.data.rows[i].id+"'>"+res.data.rows[i].cateName+"</option>");
                                    }
                                    var parentId = data2.parentId;

                                    $("#parentInfo option").each(function(){
                                        //遍历所有option
                                        var value = $(this).val();   //获取option值
                                        var text = $(this).text();
                                        if(parentId == value){
                                            $("#parentInfo").find("option[value='"+value+"']").attr("selected",true);
                                        }
                                    });
                                }
                            }
                        })
                    }
                }
            })
        }else{
            $("#parentInfo").html("");
            $.ajax({
                url:application.contextPath+"/category/queryCategoryAll",
                type: 'POST',
                dataType: 'json',
                data:{"cateType":"notice","categoryId":null},
                success: function (res) {
                    if(null != res.data){
                        $("#parentInfo").append("<option value='0'>根节点</option>");
                        for (var i=0; i<res.data.rows.length;i++) {
                            $("#parentInfo").append("<option value='"+res.data.rows[i].id+"'>"+res.data.rows[i].cateName+"</option>");
                        }
                    }
                }
            })
        }
    },
    queryAllCategory:function () {
        var cateType = $("#categoryType").val();
        $("#parentInfo").html("");
        $.ajax({
            url:application.contextPath+"/category/queryCategoryAll",
            type: 'POST',
            dataType: 'json',
            data:{"cateType":cateType,"categoryId":""},
            success: function (res) {
                if(null != res.data){
                    $("#parentInfo").append("<option value='0'>根节点</option>");
                    for (var i=0; i<res.data.rows.length;i++) {
                        $("#parentInfo").append("<option value='"+res.data.rows[i].id+"'>"+res.data.rows[i].cateName+"</option>");
                    }
                }
            }
        })
    },
    queryParentName:function () {
        $.get(
            application.contextPath+"/category/showCategoryParentNamePage",
            function (res) {
                layer.open({
                    type:1,
                    title:'添加父级名称',
                    content:res,
                    maxmin:true,//最大，最小开关
                    shade:0.5,//遮盖层模糊度
                    anim:1,//打开跟关闭动画
                    scrollbar:true,//是否设置滚轮
                    area:["1080px","500px"]
                })
            }
        )
    },
    isUserName:function(){
        //用户名
        var loginName=$.trim($("#loginName").val());
        if(jsEcc.isNull(loginName,"用户名")){
            if(jsEcc.isTrueName(loginName)){
                data.loginName=loginName;
            }else{
                return false;
            }
        }else{
            return false;
        }
        $.ajax({
            url:application.contextPath+"/user/queryUserByLoginName",
            type: 'POST',
            dataType: 'json',
            data:{userName:loginName},
            success: function (res) {
                 if('0' != res.data){
                     layer.msg("用户名称已存在",{icon:5})
                 }
            }
        })
    },
    commitUpdate:function(form){
    var data = {};
    var categoryName=$.trim($("#categoryName").val());
    if(jsEcc.isNull(categoryName)){
        data.categoryName=categoryName;
    }else{
        return false;
    }
    var categoryType=$.trim($("#categoryType").val());
    if(jsEcc.isNull(categoryType)){
        data.categoryType=categoryType;
    }else{
        return false;
    }
    var categoryLevel=$.trim($("#categoryLevel").val());
    data.categoryLevel=categoryLevel;

    var sn=$.trim($("#sn").val());
    data.sn=sn;
    data.icon=$("#imgUrl").val();

    data.parentId = $("#parentInfo").val();
    data.id = categoryId;
    $.ajax({
        url: application.contextPath+"/category/updateCategory",
        type:"POST",
        contentType: "application/json; charset=utf-8",
        dataType:"json",
        data:JSON.stringify({parmsMap:data}),
        success:function (res) {
            layer.msg('分类修改成功',{icon:1,title:'修改提示',btn:['是'],yes(){
                    layer.close(layer.index);
                    loadUtil = new Load($);
                    loadUtil.load({
                        container:'#homeContent',
                        url: application.contextPath+"/category/showCategoryPage?menuId="+menuId
                    });
                }});
        }
    });
    return false;
},
    // commitAdd:function(){
    //     var data = {};
    //     data.id = $("#cateId").val();
    //     var categoryName=$.trim($("#categoryName").val());
    //     if(jsEcc.isNull(categoryName)){
    //         data.categoryName=categoryName;
    //     }else{
    //         return false;
    //     }
    //     var categoryType=$.trim($("#categoryType").val());
    //     if(jsEcc.isNull(categoryType)){
    //         data.categoryType=categoryType;
    //     }else{
    //         return false;
    //     }
    //     var categoryLevel=$.trim($("#categoryLevel").val());
    //     data.categoryLevel=categoryLevel;
    //
    //     var sn=$.trim($("#sn").val());
    //     data.sn=sn;
    //
    //     data.parentId = $("#parentInfo").val();
    //
    //     $.ajax({
    //         url: application.contextPath+"/category/updateCategory",
    //         type:"POST",
    //         contentType: "application/json; charset=utf-8",
    //         dataType:"json",
    //         data:JSON.stringify({parmsMap:data}),
    //         success:function (res) {
    //             if(!res.isSuccess){
    //                 layer.open({
    //                     title:"分类新增失败!",
    //                     content:res.message
    //                 });
    //             }else{
    //                 layer.msg('分类新增成功',{icon:1,title:'新增提示',btn:['是'],yes(){
    //                         layer.close(layer.index);
    //                         loadUtil = new Load($);
    //                         loadUtil.load({
    //                             container:'#homeContent',
    //                             url: application.contextPath+"/category/showCategoryPage?menuId="+menuId
    //                         });
    //                     }});                }
    //
    //         }
    //     });
    //     return false;
    // },
    addCategoryInfo:function(){
        var flag = true;
        var data = {};
        data.id = $("#cateId").val();
        var categoryName=$.trim($("#categoryName").val());
        if(jsEcc.isNull(categoryName)){
            data.categoryName=categoryName;
        }else{
            flag = false;
        }
        var categoryType=$.trim($("#categoryType").val());
        if(jsEcc.isNull(categoryType)){
            data.categoryType=categoryType;
        }else{
            flag = false;
        }
        var categoryLevel=$.trim($("#categoryLevel").val());
        data.categoryLevel=categoryLevel;
        var sn=$.trim($("#sn").val());
        data.sn=sn;
        data.parentId = $("#parentInfo").val();
        if(flag){
            $.ajax({
                url: application.contextPath+"/category/addCategory",
                type:"POST",
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                data:JSON.stringify({parmsMap:data}),
                success:function (res) {
                    if(!res.isSuccess){;
                        layer.msg("分类新增失败");
                        $("#categoryPic").hide();
                    }else if("EXISTS" == res.data){
                        layer.msg("分类新增成功");
                        $("#categoryPic").show();
                    }else{
                        $("#cateId").val(res.data.id);
                        layer.msg("分类新增成功");
                        $("#categoryPic").show();
                    }
                }
            });
        }
    },
    queryOrg:function () {
        $.get(
            application.contextPath+"/user/showUserOrgPage?userId=",
            function (res) {
                layer.open({
                    type:1,
                    title:'添加组织机构',
                    content:res,
                    maxmin:true,//最大，最小开关
                    shade:0.5,//遮盖层模糊度
                    anim:1,//打开跟关闭动画
                    scrollbar:true,//是否设置滚轮
                    area:["1080px","500px"]
                })
            }
        )
    },
    clearSpan:function () {
        $("#loginName").html("");
    }

};

$(document).ready(function(){
    addCategory.switchInt();
});